<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>公共字段</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <script src="/style/public/publicStyle.js"></script>
</head>

<body>
<!--表格-->
<div class="weadmin-body">
    <div class="layui-row">
        <table class="layui-hide" id="tableDataList" lay-filter="usertab"></table>
    </div>
</div>

<!--表格操作-->
<script type="text/html" id="op">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">维度</a>
</script>

<script type="text/html" id="toolbarDemo">
    <input class="text" style="height:35px;width:257px;" name="" id="hiddenFilter" placeholder="根据表名过滤"/>
    <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="getCheckData">新增</button>
</script>

<div class="layui-row" id="tableForm" style="display:none;">
    <div class="layui-col-md11">
        <form class="layui-form layui-from-pane" id="formId" style="margin-top:20px" lay-filter="tableForm">
            <input type="hidden" name="idPubfld"/>
            <div id="fildDiv">
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">编码</label>
                                <div class="layui-input-block">
                                    <input type="text" id="cdInput" name="cd" required lay-verify="required"
                                           autocomplete="off"
                                           class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="na" required lay-verify="required" autocomplete="off"
                                           class="layui-input">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">字段类型</label>
                                <div class="layui-input-block">
                                    <select name="euJavatp" id="euJavatp"></select>
                                </div>
                                <input type="hidden" name="naJavatp" id="naJavatp"/>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">启用标记</label>
                                <div class="layui-input-block">
                                    <select name="fgAct" lay-filter="fgAct">
                                        <option value="0">停用</option>
                                        <option value="1" selected="">启用</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">长度</label>
                                <div class="layui-input-block">
                                    <input type="text" name="numLth" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">精度</label>
                                <div class="layui-input-block">
                                    <input type="text" name="numPrec" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-md12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">維度</label>
                            <div class="layui-input-block">
                                <input lay-filter="isDim" type="radio" name="fgDim" value="1" title="是" class="fgDim">
                                <input id="fgNoDim" lay-filter="isDim" type="radio" name="fgDim" value="0" title="否"
                                       class="fgDim"
                                       checked>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div id="dimDiv">
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">维度类型</label>
                                <div class="layui-input-block">
                                    <select name="sdDimtp" id="sdDimtp">
                                        <option value="1">退化维</option>
                                        <option value="2">普通维度</option>
                                        <option value="3">非校验维度</option>
                                        <option value="4" disabled>对照维度</option>
                                    </select>
                                </div>
                                <input type="hidden" name="naDimtp" id="naDimtp"/>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">维度表</label>
                                <div class="layui-input-block">
                                    <input type="text" id="cdTbDimInput" name="cdTbDim" class="layui-input">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">维度编码</label>
                                <div class="layui-input-block">
                                    <input type="text" id="cdDimInput" name="cdDim" class="layui-input">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-md12">
                        <div class="layui-form-item">
                            <div class="layui-input-block" style="text-align: center; margin-left: 0px;">
                                <button id="saveDimBut" class="layui-btn layui-btn-normal layui-btn-submit " lay-submit=""
                                        lay-filter="demo11">保存
                                </button>
                                <!--   <button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

</body>
<script>
    layui.use(['util', 'laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element'], function () {
        var enableDim = true;
        var currPage;
        var util = layui.util;
        var laydate = layui.laydate //日期
            , laypage = layui.laypage //分页
            , layer = layui.layer //弹层
            , table = layui.table //表格
            , carousel = layui.carousel //轮播
            , upload = layui.upload //上传
            , element = layui.element //元素操作
            , form = layui.form;
        var logintoken = null;
        getSharreTieba_LoginToken();
        table.render({
            elem: '#tableDataList'
            , even: true //开启隔行背景
            , cellMinWidth: 80
            , toolbar: '#toolbarDemo'
            , url: global_visit_url_prefix + '/etl/mdMedPubfld/findByPage'
            , where: {
                LoginKey: logintoken,
            }
            , cols: [[
                , {field: 'pk', title: 'pk', hide: true}
                , {field: 'idPubfld', title: '字段'}
                , {field: 'na', title: '名称'}
                , {field: 'euJavatp', title: '类型', hide: true}
                , {field: 'naJavatp', title: '字段类型', width: 151}
                , {field: 'numLth', title: '长度', width: 70}
                , {field: 'numPrec', title: '精度', width: 60}
                , {
                    field: 'fgDim', title: '维度', width: 60, templet: function (d) {
                        if (d.fgDim == '1') {
                            return '是'
                        } else {
                            return '否'
                        }

                    }
                }
                , {field: 'sdDimtp', title: '维度类型ID', width: 60, hide: true}
                , {field: 'naDimtp', title: '维度类型', width: 100}
                , {field: 'cdTbDim', title: '维度表'}
                , {field: 'cdDim', title: '维度编码'}

                , {fixed: 'right', title: '操作', align: 'center', toolbar: '#op'}
            ]]
            , page: true
            , limit: 20
            , done: function (rest, curr, count) {
                currPage = curr;
            }
        });

        $.ajax({
            url: '/etl/baseDbTpcomp/findJavaType',
            type: 'get',
            contentType: 'application/json;charset=UTF-8',
            success: function (data1) {
                var data = data1.data; //提出返回数据中的data
                $("#euJavatp").empty();//清空默认值
                if (data !== null) {
                    $.each(data, function (index, item) { //循环加入 data的元素
                        $("#euJavatp").append("<option value='" + item.cd + "'>" + item.na + "</option>");
                    });
                }
            }
        });
        var indexLay;
//监听行工具事件
        table.on('tool(usertab)', function (obj) { //注：tool 是工具条事件名，usertab 是 table 原始容器的属性 lay-filter="对应的值"

            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent == 'edit') {
                setFormValue(obj, null);
                indexLay = layer.open({
                    //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                    type: 1,
                    title: "编辑维度",
                    area: ['700px', '350px'],
                    content: $("#tableForm")//引用的弹出层的页面层的方式加载修改界面表单
                });
                //动态向表传递赋值可以参看文章进行修改界面的更新前数据的显示，当然也是异步请求的要数据的修改数据的获取
                setFormValue(obj, data);
                $("#fildDiv").hide();
                $("#dimDiv").hide();

                if (data.fgDim == '1') {
                    $("#dimDiv").show();
                }
            } else if (layEvent == 'delete') {
                layer.confirm('确定数据源要删除数据源' + data.na + '吗？', {
                    btn: ['确定', '取消'], //按钮
                }, function () {
                    layer.closeAll('dialog');
                    $.ajax({
                        type: "get",
                        url: "/etl/mdMedPubfld/delete",
                        async: true,
                        data: {'pk': data.pk},
                        success: function (json) {
                            table.reload("tableDataList");
                        }
                    });
                });
            }
        });

        $("body").on("keypress", "#hiddenFilter", function (event) {
            var content = $(this).val();
            if (event.keyCode == '13') {
                //执行重载
                table.reload('tableDataList', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        filter: content		//在表格中进行搜索
                    }
                });
                $("#hiddenFilter").val(content);
            }
        });
//监听表头
        table.on('toolbar(usertab)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    layer.open({
                        //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                        type: 1,
                        title: "新增字段",
                        area: ['700px', '630px'],
                        content: $("#tableForm")//引用的弹出层的页面层的方式加载修改界面表单
                    });
                    setFormValue(obj, null);
                    $("#fildDiv").show();
                    $("#dimDiv").hide();
                    break;
            }
        });


        form.on('radio(isDim)', function (data) {
            enableDim = true;

            if (data.value == '1') {
                var val = $("#cdInput").val();
                var valTableName = $("#cdTbDimInput").val();
                var valDimCd = $("#cdDimInput").val();
                var indexOf = val.indexOf("_");
                var prefix = val.substr(0, indexOf);
                var dimNa = val.substr(indexOf + 1);
                var tableName;
                if (prefix == 'dim') {
                    tableName = "md_" + val;
                } else if (prefix == 'id') {
                    tableName = "md_" + dimNa;
                } else {
                    alert("只允许dim与id开头的公共字段置为维度!")
                    enableDim = false;
                }

                if (valTableName == undefined || valTableName == 'undefined' || valTableName == '' || valTableName.trim() == '') {
                    $("#cdTbDimInput").val(tableName);
                }

                if (valDimCd == undefined || valDimCd == 'undefined' || valDimCd == '' || valDimCd.trim() == '') {
                    $("#cdDimInput").val(tf(dimNa));
                }
                $("#dimDiv").show();
            } else {
                $("#dimDiv").hide();
            }
        });

        //监听弹出框表单提交，massage是修改界面的表单数据'submit(demo11),是修改按钮的绑定
        function setFormValue(obj, data) {
            if (data == null) {
                $("#formId")[0].reset();
            } else {
                form.val("tableForm", data);
            }
            form.on('submit(demo11)', function (massage) {
                if (enableDim == false && massage.field.fgDim == '1') {
                    alert("只允许dim与id开头的公共字段置为维度!")
                    return false;
                }

                massage.field["naJavatp"] = $("#euJavatp").find("option:selected").text()
                massage.field["naDimtp"] = $("#sdDimtp").find("option:selected").text()

                $.ajax({
                    url: '/etl/mdMedPubfld/save',
                    type: 'post',
                    async: false,
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    data: JSON.stringify(massage.field),
                    success: function (msg) {
                        if (msg.code == 1) {
                            alert(msg.msg);
                        } else {
                            table.reload("tableDataList", {page: {curr: currPage}});
                        }
                    },
                    error: function (msg) {
                        alert("保存失败，请重试！");
                    }
                });
                layer.closeAll();
                return false;
            })
        }
    });
    var tf = function (str) {
        var arr = str.split("_");
        for (var i = 1; i < arr.length; i++) {
            arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
        }
        return arr.join("");
    };


</script>

</html>